package com.newzqxq.koalamoney.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseActivity;
import com.newzqxq.koalamoney.entity.RegisterBean;
import com.newzqxq.koalamoney.presenter.RegisterPresenter;
import com.newzqxq.koalamoney.utils.AppUtils;
import com.newzqxq.koalamoney.utils.MyToast;
import com.newzqxq.koalamoney.utils.dowmTime.DownTimer;
import com.newzqxq.koalamoney.utils.dowmTime.DownTimerListener;
import com.newzqxq.koalamoney.view.IRegisterView;
import com.newzqxq.koalamoney.widget.ClearWriteEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZQXQ-Developer on 2017/7/17.
 */

public class RegiserActivity extends BaseActivity implements IRegisterView, DownTimerListener {
    private static final int REGISTER_BACK = 1001;
    @BindView(R.id.title_mid_tv)
    TextView tvTitle;

    @BindView(R.id.reg_phone)
    ClearWriteEditText mPhoneEdit;//输入电话
    @BindView(R.id.reg_code)
    ClearWriteEditText mCodeEdit;//输入验证码
    @BindView(R.id.reg_getcode)
    Button mGetCode;//获取验证码

    @BindView(R.id.imPawdState)
    ImageView imPawdState;
    @BindView(R.id.mPasswordEdit)
    ClearWriteEditText mPasswordEdit;
    private boolean isHidePawd = true;//是否隐藏密码

    private String mPhone, mPawd;
    boolean isBright = true;
    private boolean isRequestCode = false;
    private RegisterPresenter registerPresenter;

    @Override
    protected int getContentViewId() {
        return R.layout.ui_register;
    }

    @Override
    protected void initData() {
        registerPresenter = new RegisterPresenter(this, this);
        registerPresenter.start();
    }

    @Override
    protected void initView() {
        tvTitle.setText(R.string.ui_register);

        mGetCode.setClickable(false);
        imPawdState.setBackgroundResource(R.mipmap.icon_pawd_hide);
        addEditTextListener();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void clearMemory() {

    }

    @OnClick({R.id.rg_register_next, R.id.title_relative_back, R.id.reg_getcode, R.id.rlPawdState})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rg_register_next://注册
                String phoneString = mPhoneEdit.getText().toString().trim();
                String codeString = mCodeEdit.getText().toString().trim();
                String passwordString = mPasswordEdit.getText().toString().trim();
                if (TextUtils.isEmpty(phoneString)) {
                    MyToast.shortToast(mContext, R.string.phone_number_is_null);
                    mPhoneEdit.setShakeAnimation();
                    return;
                }

                if (TextUtils.isEmpty(codeString)) {
                    MyToast.shortToast(mContext, R.string.code_is_null);
                    mCodeEdit.setShakeAnimation();
                    return;
                }
                if (codeString.contains(" ")) {
                    MyToast.shortToast(mContext, R.string.code_cannot_contain_spaces);
                    mCodeEdit.setShakeAnimation();
                    return;
                }
                if (TextUtils.isEmpty(passwordString)) {
                    MyToast.shortToast(mContext, R.string.password_is_null);
                    mPasswordEdit.setShakeAnimation();
                    return;
                }
                if (passwordString.contains(" ")) {
                    MyToast.shortToast(mContext, R.string.password_cannot_contain_spaces);
                    mPasswordEdit.setShakeAnimation();
                    return;
                }
                registerPresenter.doRegister(phoneString, codeString, getType(), passwordString);
                ///去注册
                break;
            case R.id.title_relative_back://返回
                finish();
                break;
            case R.id.reg_getcode://获取验证码
                if (TextUtils.isEmpty(mPhoneEdit.getText().toString().trim())) {
                    MyToast.longToast(mContext, R.string.phone_number_is_null);
                } else {
                    registerPresenter.getCode(mPhoneEdit.getText().toString().trim(), getType());
                    isRequestCode = true;
                    DownTimer downTimer = new DownTimer();
                    downTimer.setListener(this);
                    downTimer.startDown(60 * 1000);
                }
                break;
            case R.id.rlPawdState:
                if (isHidePawd) {
                    mPasswordEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    imPawdState.setBackgroundResource(R.mipmap.icon_pawd_hide);
                    isHidePawd = false;
                } else {
                    mPasswordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    imPawdState.setBackgroundResource(R.mipmap.icon_pawd_show);
                    isHidePawd = true;
                }
                //切换后将EditText光标置于末尾
                CharSequence charSequence = mPasswordEdit.getText();
                if (charSequence != null) {
                    Selection.setSelection((Spannable) charSequence, charSequence.length());
                }
                //刷新界面
                mPasswordEdit.postInvalidate();
                break;
        }
    }


    private void addEditTextListener() {
        mPhoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 11 && isBright) {
                    if (AppUtils.isMobile(s.toString().trim())) {
                        mPhone = s.toString().trim();
                        AppUtils.onInactive(mContext, mPhoneEdit);
                        mGetCode.setClickable(true);
                        mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.get_code_bg_red));
                    } else {
                        Toast.makeText(mContext, R.string.Illegal_phone_number, Toast.LENGTH_SHORT).show();
                    }
                } else {
                    mGetCode.setClickable(false);
                    mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.get_code_bg_gray));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mCodeEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 6) {
                    AppUtils.onInactive(mContext, mCodeEdit);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mGetCode.setText("(" + String.valueOf(millisUntilFinished / 1000) + "s)后重新获取");
        mGetCode.setClickable(false);
        mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.get_code_bg_gray));
        isBright = false;
    }

    @Override
    public void onFinish() {
        mGetCode.setText(R.string.get_code);
        mGetCode.setClickable(true);
        mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.get_code_bg_red));
        isBright = true;
    }


    @Override
    public void onShowToast(String toast) {
        MyToast.shortToast(mContext, toast);
    }

    @Override
    public void onShowLoading(boolean show) {

    }

    @Override
    public void onShowLoading(boolean show, boolean cancelAble) {

    }

    @Override
    public String getPhone() {
        return mPhoneEdit.getText().toString().trim();
    }

    @Override
    public String getCode() {
        return null;
    }

    @Override
    public String getPassword() {
        return mPasswordEdit.getText().toString().trim();
    }

    @Override
    public int getType() {
        return 1;
    }

    @Override
    public void onRegisterResult(RegisterBean.DataBean result) {
        if (result != null) {
            Intent data = new Intent();
            data.putExtra("phone", getPhone());
            data.putExtra("password", getPassword());
            setResult(REGISTER_BACK, data);
            this.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        registerPresenter.end();
    }
}
